define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product/order/index' + location.search,
                    del_url: 'product/order/del',
                    deliver_url: 'product/order/deliver',
                    info_url: 'product/order/info',
                    refund_url: 'product/order/refund',
                    table: 'order',
                },
                pageSize: 5, //调整分页大小为20
                pageList: [5, 10, 20, 30]
            });


            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                // 当访问列表发起ajax请求数据
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                // 主键
                pk: 'id',
                // 设置默认排序
                sortName: 'createtime',
                sortOrder: 'desc',
                search: false, // 取消快速搜索框
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: true },
                        { field: 'code', title: __('Code') },
                        { field: 'business.nickname', title: __('BusName'), operate: 'LIKE' },
                        { field: 'amount', title: __('Amount'), operate: '>=' },
                        { field: 'expressquery.name', title: __('ExpressName'), operate: 'LIKE' },
                        { field: 'expresscode', title: __('Expresscode') },
                        { field: 'status', title: __('Status'), searchList: { "0": __('未支付'), "1": __('已支付'), "2": __('已发货'), "3": __('已收货'), "4": __('已完成'), "-1": __('仅退款'), "-2": __('退款退货'), "-3": __('售后中'), "-4": __('审核成功'), "-5": __('审核失败') }, formatter: Table.api.formatter.status },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'info',
                                    title: '订单详情',
                                    extend: 'data-toggle="tooltip"',
                                    classname: "btn btn-xs btn-primary btn-dialog",
                                    icon: 'fa fa-eye',
                                    url: $.fn.bootstrapTable.defaults.extend.info_url,
                                },
                                {
                                    name: 'deliver',
                                    title: '发货',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: $.fn.bootstrapTable.defaults.extend.deliver_url,
                                    icon: 'fa fa-leaf',
                                    visible: function (row) {
                                        if (row.status == 1 || row.status == 2) {
                                            return true
                                        }
                                        return false
                                    },

                                },
                                {
                                    name:'refund',
                                    title: '退货审核',
                                    extend: 'data-toggle="tooltip"',
                                    classname: "btn btn-xs btn-primary btn-dialog",
                                    icon:'fa fa-check',
                                    url:$.fn.bootstrapTable.defaults.extend.refund_url,
                                    visible:function(row)
                                    {
                                        if(row.status == -1 || row.status == -2)
                                        {
                                            return true;
                                        }

                                        return false;
                                    }
                                }
                            ]
                        }
                    ]
                ]
            })
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        del: function () {
            Controller.api.bindevent();
        },
        deliver:function(){
            Controller.api.bindevent();
        },
        refund:function(){
            $('#c-refund').change(()=>{
                if($('#c-refund').val() == 0)
                {
                    $('#examinereason').show();
                }
                else
                {
                    $('#examinereason').hide();
                }
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    }

    return Controller;
});
